package org.pisces.wechat.commons.button;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

public class MenuUtils {

	public static void main(String[] args) {
		Menu menu= new Menu();
		List<Button> button = new ArrayList<>();
		
		
	/*	ClickButton clickButton = new ClickButton();
		clickButton.setKey("bind");
		clickButton.setName("绑定账号");
		button.add(clickButton);*/
		
		
		
		ViewButton viewButton = new ViewButton();
		viewButton.setUrl("http://17939a5d39.imwork.net/userinfo");
		viewButton.setName("个人中心");
		button.add(viewButton);
		
		
		
//		ComplexButton complexButton = new ComplexButton();
//		List<SimpleButton> sub_button = new ArrayList<>();
//		complexButton.setName("更多帮助");
//		ViewButton download = new ViewButton();
//		download.setUrl("http://www1.lan-x.cn");
//		download.setName("下载游戏");
//		sub_button.add(download);
//		
//		ViewButton rule = new ViewButton();
//		rule.setUrl("http://www1.lan-x.cn");
//		rule.setName("游戏规则");
//		sub_button.add(rule);
//		
//		
//		
//		complexButton.setSub_button(sub_button);
//		button.add(complexButton);
		
		menu.setButton(button);
		
		
		String jsonMenu=JSONObject.toJSONString(menu);
		
		String path="http://api.weixin.qq.com/cgi-bin/menu/create?access_token="+"9nXXigR2Fy-9Rgn4dyjoAgLpvWm9zWnc6gnY428wkWJKZHaWAunhUOeg24r5Wqc90TrFpiJzC2XZH3PlcGhdkwpolu8XD-pIcS-2nD6gI1PyNxjOoWLZt7rDPVaf-e9TYHCdAEAENI";
		
		try {
			URL url=new URL(path);
			HttpURLConnection http = (HttpURLConnection)url.openConnection();
			http.setDoOutput(true);
			http.setDoInput(true);
			http.setRequestMethod("POST");
			http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			http.connect();
			OutputStream os = http.getOutputStream();
			os.write(jsonMenu.getBytes("UTF-8"));
			os.close();
			
			InputStream is = http.getInputStream();
			int size = is.available();
			byte[] bt = new byte[size];
			is.read(bt);
			String message=new String(bt,"UTF-8");
			JSONObject jsonMsg = JSONObject.parseObject(message);
			int status = Integer.parseInt(jsonMsg.getString("errcode"));
			System.out.println(status);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
		
		System.out.println(JSONObject.toJSONString(menu));
	}
}
